<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button>开启定时器</button>
    <button>结束定时器</button>
    <script>
        var btn = document.getElementsByTagName("button");
        var timer = null;
        btn[0].onclick = function(){
            //设置周期性定时器（延迟定时器） 延迟几秒执行，在延迟几秒在执行
            timer = setInterval(fn,3000);
            console.log(timer);
        }
        btn[1].onclick = function(){
            //清除定时器
            clearInterval(timer);
            console.log(timer);
        }
        function fn(){
            console.log('hello world');
        }
    </script>
</body>
</html>